import React, { useMemo } from "react";
import EditPage, { createFields } from "../../Component/ResourceList/Edit";
import { LibAppView } from '@/lib';
import { FormattedMessage } from "react-intl";
import lodash from 'lodash';


const SchoolEdit = LibAppView.withService({
    reqEndPoints: [{
        endPoint: "setting.configList",
        name: "category",
        uri: { config: "schoolType" }
    }]
})((props) => {
    const requireRules = useMemo(() => ([{ required: true, message: <FormattedMessage id="admin.pages.article.formItem.required"/>}]), []);
    const categoryList = useMemo(() => {
        const specialTypeData =  lodash.get(props.responseData, "category.data", []);
        return specialTypeData.map((item: any) => ({
            label: item.dataValue,
            value: item.name
        }));
    }, [props.responseData]);
    const fields = useMemo(() =>createFields([{
        label: "学校名称",
        name: "name",
        type: "Input",
        required: true
    },{
        label: "封面",
        name: "cover",
        type: "Upload",
        required: true,
        rules: requireRules
    }, {
        label: "简介",
        name: "description",
        type: "TextArea"
    },{
        label: "地区",
        name: "location",
        type: "Region",
        required: true,
    },{
        label: "详细地址",
        name: "address",
        type: "Input"
    },{
        label: "类别",
        name: "type",
        type: "Select",
        required: true,
        props: {
            options: categoryList || []
        }
    }, {
        label: "内容",
        name: "context",
        type: "RichText",
        required: true
    }]),[categoryList]);
    return (
        <EditPage
            submitEndPoint="resource.editSchool"
            fields={fields}
            initValues={{}}
            buildResponse={(response: any) => {
                return {
                    ...response,
                    location: [response.province, response.city, response.area]
                }
            }}
            buildRequest={(formData: any) => {
                const regionData = formData.location || [];
                return {
                    ...formData,
                    status: formData.status ? 1 : 0,
                    province: regionData[0],
                    city: regionData[1],
                    area: regionData[2]
                };
            }}
        />
    )
});

export default SchoolEdit;

